<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click=" $router.push('/goodsadd')">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableList" stripe style="width: 100%" border>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70"></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="stData">
            <span>{{stData.row.add_time | getTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="yecan.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="yecan.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tot"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created () {
    this.getTableList()
  },
  data () {
    return {
      // 分页器总条数
      tot: 0,
      yecan: {
        pagenum: 1,
        pagesize: 5
      },
      search: '',
      tableList: [],
      tableCan: {
        query: '',
        pagenum: 1,
        pagesize: 5
      }
    }
  },
  methods: {
    // 分页器每一面显示的数据条数 发生变化的回调函数
    handleSizeChange (val) {
      this.tableCan.pagesize = val
      this.getTableList()
    },
    // 分页器当前页面发生跳转(页码变化)的回调函数
    handleCurrentChange (val) {
      this.tableCan.pagenum = val

      this.getTableList()
    },
    async getTableList () {
      var { data: dt } = await this.$http.get('goods', {
        params: this.tableCan
      })
      console.log(dt)
      this.tableList = dt.data.goods
      // 把数据中获得的总条数复制给分页器
      this.tot = dt.data.total
    }
  },
  filters: {
    getTime (oldTime) {
      var addZero = n => (n < 10 ? '0' + n : n)

      var date = new Date(oldTime)
      var y = addZero(date.getFullYear())
      var m = addZero(date.getMonth() + 1)
      var d = addZero(date.getDate())
      var h = addZero(date.getHours())
      var min = addZero(date.getMinutes())
      var s = addZero(date.getMinutes())

      return `${y}年${m}月${d}日 ${h}:${min}:${s}`
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  font-size: 12px;
}
</style>
